package com.hellpro.lolbox;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hellpro.lolbox.acticity.presenter.MainActivityPresenter;
import com.hellpro.lolbox.acticity.view.IMainActivity;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.factory.MainFragmentFactory;
import com.hellpro.lolbox.utils.DrawableSetting;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends LolBaseActivity implements IMainActivity {


    @BindView(R.id.rl_content)
    FrameLayout rlContent;
    @BindView(R.id.main_rdb_home)
    RadioButton mainRdbHome;
    @BindView(R.id.main_rdb_video)
    RadioButton mainRdbVideo;
    @BindView(R.id.main_rdgOne)
    RadioGroup mRg;
    @BindView(R.id.main_rdb_moment)
    RadioButton mainRdbMoment;
    @BindView(R.id.main_rdb_mine)
    RadioButton mainRdbMine;
    @BindView(R.id.main_iv_content)
    ImageView mainIvContent;
    @BindView(R.id.main_img)
    RadioButton mainImg;
    @BindView(R.id.main_flContent)
    FrameLayout mainFlContent;
    @BindView(R.id.main_iv_dismiss)
    ImageView mainIvDismiss;
    @BindView(R.id.main_type_normal)
    TextView mainTypeNormal;
    @BindView(R.id.main_type_envelop)
    TextView mainTypeEnvelop;
    @BindView(R.id.main_type_video)
    TextView mainTypeVideo;
    @BindView(R.id.main_type_audio)
    TextView mainTypeAudio;
    @BindView(R.id.main_type_live)
    TextView mainTypeLive;
    @BindView(R.id.main_type_link)
    TextView mainTypeLink;

    private List<Fragment> fragments;
    private int currentIndex = 0;
    private static boolean isExit = false;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
    private RadioButton[] buttons;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void initView() {
        initData();
        //加载默认的Fragment
        getSupportFragmentManager().
                beginTransaction()
                .add(R.id.rl_content, fragments.get(0))
                .commit();


        //实现中间页面的六个功能
        MainActivityPresenter presenter = new MainActivityPresenter(this);
        presenter.startContent();
        //缩小底部按钮
        new DrawableSetting(this,mainRdbHome,mainRdbVideo,mainRdbMoment,mainRdbMine,60,60);

    }

    /**
     * 初始化数据
     */
    private void initData() {
        fragments = new ArrayList<>();
        buttons = new RadioButton[mRg.getChildCount()];
        for (int i = 0; i < mRg.getChildCount(); i++) {
            buttons[i] = (RadioButton) mRg.getChildAt(i);
            Fragment fragment = MainFragmentFactory.createFragment(i);
            fragments.add(fragment);
        }
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < mRg.getChildCount(); i++) {
                    if (buttons[i].getId() == checkedId) {
                        if (R.id.main_img == checkedId) {
                            mainImg.setEnabled(false);
                        } else {
                            switchFragment(i);
                        }
                    }
                }
            }
        });
        //设置显示中间布局，处理事件拦截
        mainIvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainFlContent.setVisibility(View.VISIBLE);
                mainFlContent.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return true;
                    }
                });
            }
        });
        //设置隐藏中间布局，处理事件拦截
        mainIvDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainFlContent.setVisibility(View.GONE);
                mainFlContent.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return true;
                    }
                });
            }
        });
    }

    /**
     * 每个fragment的之间的交换
     *
     * @param
     */
    private void switchFragment(int id) {
        //目标fragment
        Fragment fragment = fragments.get(id);
        //当前fragment
        Fragment fragment1 = fragments.get(currentIndex);
        //开启一个事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment.isAdded()) {
            transaction.hide(fragment1).show(fragment);
        } else {
            transaction.hide(fragment1).add(R.id.rl_content, fragment);
        }
        transaction.commit();
        currentIndex = id;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public TextView getNormal() {
        return mainTypeNormal;
    }

    @Override
    public TextView getRed() {
        return mainTypeEnvelop;
    }

    @Override
    public TextView getVideo() {
        return mainTypeVideo;
    }

    @Override
    public TextView getAudio() {
        return mainTypeAudio;
    }

    @Override
    public TextView getLive() {
        return mainTypeLive;
    }

    @Override
    public TextView getLink() {
        return mainTypeLink;
    }

    @Override
    public Context getMainContext() {
        return this;
    }
   /* //处理返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }*/


}
